<?php
namespace app\admin\model;

use think\Db;
use think\Model;
use function mooshroom\pr;

class Menu extends Model
{

    protected $name = 'role_menu';
    protected $autoWriteTimestamp = 'datetime';
    public function add($data)
    {
        return $this->allowField(true)->save($data);
    }
    public function edit($id,$data)
    {
        return $this->allowField(true)->save($data,['id'=>$id]);
    }
    public static function getMenuInfo($id){
        return self::get($id);
    }
    public static function getMenuList($is_show=1,$field='*'){
            if($is_show !=2){

                $where = [["is_show","=",$is_show]];
                if(ADMINTYPE ==2) {
                    $where[] = ["is_join","=",1];
                }
                $data = self::where($where)->field($field)->order('list_order','asc')->select();
            } else
                $data = self::field($field)->order('list_order','asc')->select();
       return self::cation($data);
    }
    public static function getMenuIn($name,$in,$field='*'){
       return self::whereIn($name,$in)->field($field)->order('list_order','asc')->select();
    }


    private  static function cation($arr,$num=0,$m=1,$router_id='id')
    {
        $list = [];
        foreach($arr as $k=>$v){
            if($v['parent_id'] === $num){
                $v['level'] = $m;
                $v['son'] = self::cation($arr,$v[$router_id],$m+1);
                $list[] = $v;
            }
        }
        return $list;
    }

}